<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>父子组件传值</title>
  <script src="./vue.js"></script>
</head>
<body>
  <div id="root">
    <counter :count="2" @change="handleChange"></counter>
    <counter :count="3" @change="handleChange"></counter>
    {{total}}
  </div>
  <script>
    // 父组件向子组件传值,子组件通过data将父组件传过来的值复制一遍再使用
    var counter = {
      props: ['count'],
      data: function() {
        return {
          num: this.count
        };
      },
      template: "<div @click='handleClick'>{{num}}</div>",
      methods: {
        handleClick: function() {
          this.num = this.num + 2;
          this.$emit("change", 2); // change事件名、2传递参数，单词累加步长
        }
      }
    }
    var vm = new Vue({
      el: "#root",
      components: {
        counter: counter
      },
      data: {
        total: 5
      },
      methods: {
        handleChange: function(step) { // step接收子组件传递过来的参数2
          this.total += step;
        }
      }
    })
  </script>
</body>
</html>